package com.school.gmz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.gmz.common.Result;
import com.school.gmz.entity.StuFdBack;
import com.school.gmz.mapper.StuFdBackMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/stufdback")
@Api(tags = "学生反馈管理") // 定义API的分类
public class StuFdBackController {
    @Resource
    StuFdBackMapper stufdbackMapper;

    // 添加学生反馈
    @PostMapping
    @ApiOperation(value = "添加学生反馈", notes = "创建一个新的学生反馈记录")
    public Result<?> save(@RequestBody StuFdBack stufdback){
        stufdbackMapper.insert(stufdback);
        return Result.success();
    }

    // 更新学生反馈
    @PutMapping
    @ApiOperation(value = "更新学生反馈", notes = "根据ID更新学生反馈记录")
    public Result<?> update(@RequestBody StuFdBack stufdback){
        stufdbackMapper.updateById(stufdback);
        return Result.success();
    }

    // 删除学生反馈
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除学生反馈", notes = "根据ID删除学生反馈记录")
    public Result<?> delete(@ApiParam(value = "学生反馈ID", required = true) @PathVariable Long id){
        stufdbackMapper.deleteById(id);
        return Result.success();
    }

    // 查询学生反馈
    @GetMapping
    @ApiOperation(value = "查询学生反馈", notes = "支持姓名、班级、年级和关键字查询")
    public Result<?> findPage(
            @ApiParam(value = "姓名", defaultValue = "") @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "班级", defaultValue = "") @RequestParam(defaultValue = "") String classes,
            @ApiParam(value = "年级", defaultValue = "") @RequestParam(defaultValue = "") String grade,
            @ApiParam(value = "搜索关键字", defaultValue = "") @RequestParam(defaultValue = "") String keyword){
        LambdaQueryWrapper<StuFdBack> wrapper = Wrappers.<StuFdBack>lambdaQuery();
        if (StrUtil.isNotBlank(name)){
            wrapper.like(StuFdBack::getName, name); // 假设字段名为name
        }
        if (StrUtil.isNotBlank(classes)){
            wrapper.like(StuFdBack::getClasses, classes); // 假设字段名为className
        }
        if (StrUtil.isNotBlank(grade)){
            wrapper.like(StuFdBack::getGrade, grade); // 假设字段名为grade
        }
        if (StrUtil.isNotBlank(keyword)){
            wrapper.like(StuFdBack::getFdback, keyword); // 假设字段名为keyword
        }
        Page<StuFdBack> stufdBackPage = stufdbackMapper.selectPage(new Page<>(1, 10), wrapper); // 默认分页参数
        return Result.success(stufdBackPage);
    }
}